package com.tzstcl.sys.user.ctrl;

import com.tzstcl.base.ctrl.BaseCtrl;
import com.tzstcl.base.model.AjaxResult;
import com.tzstcl.commons.utils.StringUtils;
import com.tzstcl.framework.constants.AppConstants;
import com.tzstcl.sys.user.model.SysDept;
import com.tzstcl.sys.user.service.SysDeptService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门机构控制器
 *
 * @author haocj
 */
@Controller
@RequestMapping(value = "/admin/sysDept")
public class SysDeptCtrl extends BaseCtrl {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * list页面导航
     *
     * @return
     */
    @RequiresPermissions("sysDept:view")
    @RequestMapping("/toList")
    public String toList() {
        return "admin/sys/user/sysDeptList";
    }

    /**
     * 获取查询的分页数据
     *
     * @param sysDept
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions("sysDept:view")
    @ResponseBody
    public List<SysDept> list(SysDept sysDept) {
        return sysDeptService.selectList(sysDept);
    }

    /**
     * form页面导航
     *
     * @return
     */
    @RequiresPermissions(value = {"sysDept:edite", "sysDept:add"}, logical = Logical.OR)
    @RequestMapping("/toForm")
    public String toForm(@RequestParam(value = "id", required = false) Long id, Model model) {
        if (null != id) {
            model.addAttribute("sysDept", sysDeptService.getOne(id));
        }
        return "admin/sys/user/sysDeptForm";
    }

    /**
     * form页面导航 详情
     *
     * @return
     */
    @RequiresPermissions("sysDept:view")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, Model model) {
        if (null != id) {
            SysDept dept = sysDeptService.getOne(id);
            if (null != dept && dept.getParentId() != 0) {
                SysDept parentDept = sysDeptService.getOne(dept.getParentId());
                if (null != parentDept) {
                    dept.setParentName(parentDept.getDeptName());
                }
            }
            model.addAttribute("sysDept", dept);
        }
        return "admin/sys/user/sysDeptDetail";
    }

    /**
     * 新增
     *
     * @param sysDept
     * @return
     */
    @RequestMapping("/add")
    @RequiresPermissions(value = {"sysDept:add"})
    @ResponseBody
    public AjaxResult save(@Valid SysDept sysDept) {
        return toAjax(sysDeptService.add(sysDept));
    }

    /**
     * form页面导航 更新
     *
     * @return
     */
    @RequiresPermissions("sysDept:edite")
    @GetMapping("/edit/{id}")
    public String toUpdate(@PathVariable("id") Long id, Model model) {
        if (null != id) {
            SysDept dept = sysDeptService.getOne(id);
            if (null != dept && dept.getParentId() != 0) {
                SysDept parentDept = sysDeptService.getOne(dept.getParentId());
                if (null != parentDept) {
                    dept.setParentName(parentDept.getDeptName());
                }
            }
            model.addAttribute("sysDept", dept);
        }
        return "admin/sys/user/sysDeptEdit";
    }

    /**
     * 更新
     *
     * @param sysDept
     * @return
     */
    @PostMapping("/update")
    @RequiresPermissions("sysDept:edite")
    @ResponseBody
    public AjaxResult update(@Valid SysDept sysDept) {
        return toAjax(sysDeptService.update(sysDept));
    }

    /**
     * 删除多个
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deletes")
    @RequiresPermissions("sysDept:delete")
    @ResponseBody
    public AjaxResult deletes(String ids) {
        return toAjax(sysDeptService.deleteBatchIds(ids));
    }

    /**
     * @description 删除单个
     * @author guohy
     * @date 2019/11/6-15:47
     * @param id
     * @return com.tzstcl.base.model.AjaxResult
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sysDept:delete")
    @ResponseBody
    public AjaxResult delete(Long id) {
        return toAjax(sysDeptService.delete(id));
    }

    /**
     * 获取单条信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public AjaxResult get(Long id) {
        return success("获取信息成功", sysDeptService.getOne(id));
    }

    /**
     * 校验唯一性
     * 权限配置为：多权限任选一，有新增和修改其一权限就可以访问
     *
     * @param sysDept
     * @return
     */
    @PostMapping("/checkUnique")
    @RequiresPermissions(value = {"sysDept:add", "sysDept:edite"}, logical = Logical.OR)
    @ResponseBody
    public Map<String, Boolean> checkUnique(SysDept sysDept) {
        Map<String, Boolean> result = new HashMap<String, Boolean>(4);
        result.put("valid", true);
        Long id = sysDept.getId();
        sysDept.setId(null);
        List<SysDept> sysDeptList = sysDeptService.selectList(sysDept);
        if (StringUtils.isNotEmpty(sysDeptList)) {
            if (sysDeptList.size() > 1) {
                result.put("valid", false);
            } else {
                if (id != null) {
                    // 新增
                    result.put("valid", false);
                } else {
                    // 修改
                    if (!id.equals(sysDeptList.get(0).getId())) {
                        result.put("valid", false);
                    }
                }
            }
        }
        return result;
    }


    @RequestMapping(value = "/getById")
    @ResponseBody
    public SysDept getById(SysDept sysDept) {
        SysDept dept = sysDeptService.get(sysDept);
        SysDept sd = sysDeptService.getOne(dept.getParentId());
        if (sd != null) {
            dept.setParentName(sd.getDeptName());
        }
        return dept;
    }

    /**
     * @param
     * @return com.seasoft.base.model.AjaxResult
     * @description 组织关系节点
     * @author guohy
     * @date 2019/8/7-14:21
     */
    @RequestMapping(value = "/zTreeNodes")
    @ResponseBody
    public AjaxResult getZTreeNodes() {
        ArrayList<Map<String, Serializable>> deptList = new ArrayList<>();
        Map<String, java.io.Serializable> map = null;
        List<SysDept> list = sysDeptService.deptListForStatus();
        for (SysDept dept : list) {
            map = new HashMap<>(4);
            map.put("id", dept.getId() == null ? dept.getId() : dept.getId().toString());
            map.put("pId", dept.getParentId() == null ? dept.getParentId() : dept.getParentId().toString());
            map.put("name", dept.getDeptName());
            deptList.add(map);
        }
        return success(AppConstants.SUCCESS_MSG, deptList);
    }


    @RequestMapping(value = "/getDataForSelect")
    @ResponseBody
    public List getDataForSelect() {
        return sysDeptService.getDataForSelect();
    }

    /**
     * @param id
     * @return java.util.Map<java.lang.String, java.lang.Boolean>
     * @description 检查部门下是否还有子部门
     * @author guohy
     * @date 2019/8/7-17:52
     */
    @PostMapping("/checkDeptNode")
    @ResponseBody
    public Map<String, Boolean> checkDeptNode(Long id) {
        Map<String, Boolean> result = new HashMap<>(4);
        result.put("valid", true);
        SysDept sysDept = new SysDept();
        sysDept.setParentId(id);
        List<SysDept> deptList = sysDeptService.selectList(sysDept);
        if (deptList.size() > 0) {
            result.put("valid", false);
        }
        return result;
    }
}
